//针对树形结构给每个对象创建label--name和value--id(只添加label和value属性)
// 分别对应name和id
// parentKey为父级的属性，parentOrgId
export const treeChangeToCascaderData=(data, parentKey, propertyKey, propertyValue, pids)=>{
  if (Object.prototype.toString.call(data).slice(8, 13) != 'Array') return;
  propertyKey = propertyKey || 'id';
  propertyValue = propertyValue || 'name'
  for (let i = 0; i < data.length; i++) {
    data[i].label = data[i][propertyValue];
    data[i].value = data[i][propertyKey];
    if (data[i][parentKey] == -1 || data[i][parentKey] == '' || data[i][parentKey] == null) { data[i].pids = []; pids = [] }
    data[i].pids = pids.concat(data[i].id);
    if (data[i].children && data[i].children.length) {
      treeChangeToCascaderData(data[i].children, parentKey, propertyKey, propertyValue, data[i].pids)
    } else {
      delete data[i].children //当级联中，如果children为空时，会报错
    }
  }
  return data;
}
//根据子ID获取所有父ID
export const getIdsFromID = (id, tree)=> {
  //先将树形打平
  let list = makeTreeToListData(tree);
  let item = list.filter(item => item.id == id)[0];
  if (item) {
    return item.pids
  } else {
    return []
  }
}
//将树形转化为平铺数据
export const makeTreeToListData = (data, list = [])=> {
  data.forEach(item => {
    list.push(item);
    if (item.children && item.children.length) {
      makeTreeToListData(item.children, list)
    }
  })
  return list;
}
export const handlerSelectOptionShow = (options,value)=>{
  let colors = ['#2b85e4','#8bc34a','#c62828','#ed4014','#ff9900','#2db7f5','#2db7f5','#cddc39']
  let option = options.filter(m=>m.value==value)[0]||{};
  return {
    text:option.label,
    color:colors[option.value]
  };
}

export default {
  treeChangeToCascaderData,
  getIdsFromID,
  makeTreeToListData,
  handlerSelectOptionShow
}
